package com.jin.blog.core;

import java.util.TimeZone;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.FakeStaticHandler;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jin.blog.controller.ArticleController;
import com.jin.blog.controller.IndexController;
import com.jin.blog.controller.ProjectController;
import com.jin.blog.controller.TagController;
import com.jin.blog.controller.admin.MessageController;
import com.jin.blog.lucene.ArticleLuceneService;
import com.jin.blog.pojo.Article;
import com.jin.blog.pojo.Message;
import com.jin.blog.pojo.Project;
import com.jin.blog.pojo.User;
import com.qiniu.api.config.Config;

import freemarker.template.TemplateModelException;

public class BlogConfig extends JFinalConfig {

	private static final Logger log = Logger.getLogger(BlogConfig.class);

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("conf.properties");

		me.setDevMode(true);
		me.setBaseViewPath("/WEB-INF/page");
		me.setError404View("/404.html");
		me.setError500View("/500.html");
	}

	@Override
	public void configRoute(Routes me) {
		
		me.add("/", IndexController.class);
		me.add("/article", ArticleController.class,"article");
		me.add("/project", ProjectController.class,"/");
		me.add("/tag", TagController.class, "/");

		me.add("/admin", com.jin.blog.controller.admin.IndexController.class,"admin");
		me.add("/admin/article", com.jin.blog.controller.admin.ArticleController.class, "admin");
		me.add("/admin/project", com.jin.blog.controller.admin.ProjectController.class,"admin");
		me.add("/admin/message", MessageController.class,"admin");
	}

	@Override
	public void configPlugin(Plugins me) {
		me.add(new EhCachePlugin());
		String dbname, username, password, host, port;
		dbname = getProperty("dbname");
		username = getProperty("username", "root");
		password = getProperty("password", "root");
		host = getProperty("host", "localhost");
		port = getProperty("port", "3306");
		C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://" + host + ":" + port
				+ "/" + dbname, username, password);
		me.add(cp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
		arp.setShowSql(true);
		me.add(arp);
		arp.addMapping("user", User.class);
		arp.addMapping("article", Article.class);
		arp.addMapping("project", Project.class);
		arp.addMapping("message", Message.class);
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new UrlSkipHandler("\\S*ueditor\\S*", true));
		me.add(new FakeStaticHandler());
		me.add(new ContextPathHandler("path"));
	}

	@Override
	public void afterJFinalStart() {
		// 设置默认时间为北京时间
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
		BlogConstants.PAGE_SIZE = getPropertyToInt("pageSize");
		BlogConstants.DOMAIN = getProperty("domain");
		BlogConstants.EMAIL_USERNAME = getProperty("email_username");
		BlogConstants.EMAIL_PASSWORD = getProperty("email_password");
		BlogConstants.EMAIL_PROTOCOL = getProperty("email_protocol", "smtp");
		BlogConstants.EMAIL_HOST = getProperty("email_host");
		Config.ACCESS_KEY = getProperty("AK");
		Config.SECRET_KEY = getProperty("SK");
		JSONArray jsonArr = JSON.parseArray(getProperty("links"));
		if (StringKit.isBlank(BlogConstants.EMAIL_USERNAME)) {
			System.out.println("ERROR:用户名为空!!!");
		}
		if (StringKit.isBlank(BlogConstants.EMAIL_PASSWORD)) {
			System.out.println("ERROR:邮箱密码为空!!!");
		}
		if (StringKit.isBlank(BlogConstants.EMAIL_HOST)) {
			System.out.println("ERROR:邮箱服务器为空!!!");
		}
		try {
			FreeMarkerRender.getConfiguration().setSharedVariable("domain", BlogConstants.DOMAIN);
			FreeMarkerRender.getConfiguration().setSharedVariable("blogroll", jsonArr);
		} catch (TemplateModelException e) {
			log.error("set freemarkerrender share variable title failed", e);
		}
		ArticleLuceneService.me().indexAll();
	}
	
//	public static void main(String[] args) {
//		JFinal.start("WebRoot", 80, "/", 5);
//	}

}
